﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

using ScrumTable.Common.Threading;

#endregion

namespace ScrumTable.Common.EventHandling
{
    /// <summary>
    /// Contains a synchronized implementation of an event handler list.
    /// </summary>
    /// <typeparam name="T">Type of the delegate to use.</typeparam>
    public class SyncDelegateHandlerCollection<T> : DisposeBase, IDelegateHandlerCollection<T>
        where T : class
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IDelegateHandlerCollection<T> _toSync;
        private readonly ReaderWriterLock _lock = new ReaderWriterLock();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DelegateHandlerCollection instance.
        /// </summary>
        public SyncDelegateHandlerCollection()
            : this(new DelegateHandlerCollection<T>())
        {
        }

        /// <summary>
        /// Creates a new DelegateHandlerCollection instance.
        /// </summary>
        private SyncDelegateHandlerCollection(IDelegateHandlerCollection<T> toSync)
        {
            _toSync = toSync;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Checks whether the event with the given key has elements to fire.
        /// </summary>
        /// <param name="key">Key of the delegate to check.</param>
        /// <returns>Returns true if the corresponding event has elements to fire.</returns>
        public bool HasEvents(string key)
        {
            using (_lock.AcquireReaderLock())
            {
                return _toSync.HasEvents(key);
            }
        }

        /// <summary>
        /// Adds a new delegate to the current list.
        /// </summary>
        /// <param name="key">Key of the delegate to add.</param>
        /// <param name="toAdd">Delegate (method pointer) to add.</param>
        public void Add(string key, T toAdd)
        {
            using (_lock.AcquireWriterLock())
            {
                _toSync.Add(key, toAdd); 
            }
        }

        /// <summary>
        /// Removes a delegate form the current list.
        /// </summary>
        /// <param name="key">Key of the delegate to remove.</param>
        /// <param name="toRemove">Delegate (method pointer) to remove.</param>
        public bool Remove(string key, T toRemove)
        {
            using (_lock.AcquireWriterLock())
            {
                return _toSync.Remove(key, toRemove);
            }
        }

        /// <summary>
        /// Tries to fire a multicast delegate as an event.
        /// </summary>
        /// <param name="key">Key of the delegate to fire.</param>
        /// <param name="sender">Specifies the sender object which should be passed to the delegate.</param>
        /// <param name="args">Specifies the event argument object which should be passed to the delegate.</param>
        /// <returns>
        /// Returns true if the multicast delegate could be fired otherwise false.
        /// </returns>
        public bool TryFire(string key, object sender, EventArgs args)
        {
            using (_lock.AcquireReaderLock())
            {
                return _toSync.TryFire(key, sender, args);
            }
        }

        /// <summary>
        /// Fires a multicast delegate as an event.
        /// </summary>
        /// <param name="key">Key of the delegate to fire.</param>
        /// <param name="sender">Specifies the sender object which should be passed to the delegate.</param>
        /// <param name="args">Specifies the event argument object which should be passed to the delegate.</param>
        /// <returns>
        /// Returns true if the multicast delegate could be fired otherwise false.
        /// </returns>
        public void Fire(string key, object sender, EventArgs args)
        {
            using (_lock.AcquireReaderLock())
            {
                _toSync.Fire(key, sender, args);
            }
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            using (_lock.AcquireWriterLock())
            {
                _toSync.Dispose();
            }
        }

        #endregion
    }
}
